﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Payroll.BusinessLogic.CustomerManager;
using PayrollHR.Web;
using Payroll.Common.Utils;
using Payroll.BusinessLogic;

namespace PayrollHR.Administration.Modules
{
    public partial class CustomerSearchControl : BasePayrollAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.FillCustType();
                this.FillContriesDropDowns();
                this.FillYearDropDowns();
                this.FillMonthDropDowns();
                this.FillYearOfBirthDropDowns();
                this.RdButtonListVisa.SelectedIndex = 6;
                this.RdButtonLisStudyTravel.SelectedIndex = 4;
                this.BindFilter();
            }
        }
        public void BindFilter()
        {
            int UserId = PayrollContext.Current.Session.EmployeeId;
            hfdUserId.Value = CommonHelper.ToString(UserId);
            CustomerSearchResult customer = this.CustomerService.GetCustomerSearchResultbyId(UserId);
            if (!CommonHelper.IsNull(customer))
            {
                ddlCustType.SelectedValue = CommonHelper.ToString(customer.CutomerTypeId);
                txtCustCaseWork.Text = customer.CustCaseWork;
                txtCustomerName.Text = customer.CustName;
                txtCustHour.Text = customer.CustHour;
                txtMobile.Text = customer.CustMobile;
                txtCounselor.Text = customer.CustCounselor;
                RdButtonLisStudyTravel.SelectedIndex = customer.IsStudy;
                RdButtonListVisa.SelectedIndex = customer.statusVisa;
                txtCustStudy.Text = customer.CustSchool;
                txtProCurator.Text = customer.ProCurator;
                ddlNational.SelectedValue = CommonHelper.ToString(customer.CustNationalId);
            }
        }
        public void FillCustType()
        {
            ddlCustType.Items.Clear();
            var customertypeSelection = this.CustomerService.GetCustomerType();
            customertypeSelection.Insert(0, new CutomerType { CutomerTypeId = 0, CutomerTypeName = "All" });
            foreach (var customertype in customertypeSelection)
            {
                ListItem ddlCustTypeItem2 = new ListItem(customertype.CutomerTypeName, customertype.CutomerTypeId.ToString());
                ddlCustType.Items.Add(ddlCustTypeItem2);
            }
        }
        private void FillYearOfBirthDropDowns()
        {
            ddlYearOfBrirth.Items.Clear();
            int Min = DateTime.Now.Year - 60;
            int Max = DateTime.Now.Year - 10;
            ddlYearOfBrirth.Items.Add(new ListItem("-None-", "0"));
            for (int i = Max; i >= Min; i--)
            {
                ListItem ddlOfBrirthItem2 = new ListItem(CommonHelper.ToString(i), CommonHelper.ToString(i));
                ddlYearOfBrirth.Items.Add(ddlOfBrirthItem2);
            }
            ddlYearOfBrirth.SelectedIndex = 0;
        }
        private void FillYearDropDowns()
        {
            ddlYear.Items.Clear();
            int year = DateTime.Now.Year;
            ddlYear.Items.Add(new ListItem("-None-", "0"));
            for (int i = year; i >= 2005; i--)
            {
                ListItem ddlYearItem2 = new ListItem(CommonHelper.ToString(i), CommonHelper.ToString(i));
                ddlYear.Items.Add(ddlYearItem2);
            }
            ddlYear.SelectedIndex = 0;
        }
        private void FillMonthDropDowns()
        {
            ddlMonth.Items.Clear();
            ddlMonth.Items.Add(new ListItem("-None-", "0"));
            ddlMonthOfBrirth.Items.Clear();
            ddlMonthOfBrirth.Items.Add(new ListItem("-None-", "0"));
            for (int i = 1; i <= 12; i++)
            {
                ListItem ddlMonthItem2 = new ListItem(CommonHelper.ToString(i), CommonHelper.ToString(i));
                ddlMonth.Items.Add(ddlMonthItem2);
                ddlMonthOfBrirth.Items.Add(ddlMonthItem2);
            }
            ddlMonth.SelectedIndex = 0;
        }
        private void FillContriesDropDowns()
        {
            ddlNational.Items.Clear();
            var languageCollection = this.CustomerService.GetObjectById(1);
            if (languageCollection.Count != 1)
            {
                languageCollection.Insert(0, new Objects { ObjectId = 0, ObjectName = "-None-" });
            }
            foreach (Objects language in languageCollection)
            {
                ListItem ddlChannelItem2 = new ListItem(language.ObjectName, language.ObjectId.ToString());
                ddlNational.Items.Add(ddlChannelItem2);
            }
        }
       
        public CustomerSearchResult GetCustomerSearch()
        {
            int customerTypeId = 0;
            int CustNationalId = 0;
            int Year = 0;
            int Month = 0;
            if (ddlCustType.SelectedIndex > -1)
            {
                customerTypeId = int.Parse(ddlCustType.SelectedItem.Value);
            }
            if (ddlNational.SelectedIndex > -1)
            {
                CustNationalId = int.Parse(ddlNational.SelectedItem.Value);
            }
            if (ddlYear.SelectedIndex > 0)
            {
                Year = CommonHelper.ToInteger(ddlYear.SelectedItem.Value);
            }
            if (ddlMonth.SelectedIndex > 0)
            {
                Month = CommonHelper.ToInteger(ddlMonth.SelectedItem.Value);
            }
            string CustYearOfBirth = "0";
            if (ddlYearOfBrirth.SelectedIndex > 0)
            {
                CustYearOfBirth = ddlYearOfBrirth.SelectedItem.Value;
            }
            string CustMonthOfBirth = "0";
            if (ddlMonthOfBrirth.SelectedIndex > 0)
            {
                CustMonthOfBirth = ddlMonthOfBrirth.SelectedItem.Value;
            }
            CustomerSearchResult customer = new CustomerSearchResult
            {
                CustName = txtCustomerName.Text.Trim(),
                CustCaseWork = txtCustCaseWork.Text.Trim(),
                CustHour = txtCustHour.Text.Trim(),
                CustMobile = txtMobile.Text.Trim(),
                CustYearOfBirth = CustYearOfBirth,
                CustMonthOfBirth = CustMonthOfBirth,
                CustParent = txtCustParent.Text.Trim(),
                IsStudy = RdButtonLisStudyTravel.SelectedIndex,
                CustNationalId = CustNationalId,
                CustCounselor = txtCounselor.Text.Trim(),
                CutomerTypeId = customerTypeId,
                statusVisa = RdButtonListVisa.SelectedIndex,
                CustSchool = txtCustStudy.Text.Trim(),
                ProCurator = txtProCurator.Text.Trim(),
                CustYear = Year,
                CustMonth = Month,
                UserId = CommonHelper.ToInteger(hfdUserId.Value)
            };            
            return customer;
        }
    }
}